{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "-1"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "\n",
    "# 直方图统计\n",
    "def ImageHistCount(image,type):\n",
    "    color = (255,255,255)\n",
    "    windowName = 'Gray'\n",
    "    if type == 31:\n",
    "        color = (255,0,0)\n",
    "        windowName = 'blue Hist'\n",
    "    elif type == 32:\n",
    "        color = (0,255,0)\n",
    "        windowName = 'green Hist'\n",
    "    elif type == 33:\n",
    "        color = (0,0,255)\n",
    "        windowName = 'red Hist'\n",
    "    # 进行直方图统计\n",
    "    # 参数1：图片 参数2：计算直方图通道，此处计算灰度所以用第一个通道即可[0] 参数3：mask模板 \n",
    "    # 参数4：直方图份数 参数5： 表示直方图中各个像素的值的范围\n",
    "    histCount = cv2.calcHist([image],[0],None,[256],[0.0,255.0])\n",
    "    # 直方图中最大值以及最小值以及各自对应的坐标\n",
    "    minV,maxV,minL,maxL = cv2.minMaxLoc(histCount)\n",
    "    # 创建画布\n",
    "    histImg = np.zeros([256,256,3],np.uint8)\n",
    "    # h为高度\n",
    "    for h in range(256):\n",
    "        # 归一化处理 值归一到0-256\n",
    "        intenNormal = int(histCount[h]*256/maxV)\n",
    "        cv2.line(histImg,(h,256),(h,256-intenNormal),color)\n",
    "    cv2.imshow(windowName,histImg)\n",
    "    return histImg\n",
    "\n",
    "img = cv2.imread('image0.jpg',1)\n",
    "# 将rgb图像分解成三个颜色通道\n",
    "channels = cv2.split(img)\n",
    "for i in range(0,3):\n",
    "    ImageHistCount(channels[i],31+i)\n",
    "cv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
